package goo

import (
	"strings"
	"sync"
)

var config *Config
var onceConfig sync.Once

type ConfigInterface interface {
	ConfigConstant(config *Config)
	ConfigPlugin()
	ConfigRoute(routes *Routes)
	ConfigInterceptor()
	AfterStart()
}

type Config struct {
	StaticDir map[string]string
}

func ConfigInstance() *Config {
	onceConfig.Do(func() {
		config = &Config{StaticDir: make(map[string]string)}
	})
	return config
}

func (config *Config) SetStaticMapping(staticPrefix string, staticDir string) {
	config.StaticDir[staticPrefix] = staticDir
	RoutesInstance().Get(staticPrefix, staticHandle)
}

func (config *Config) SetStaticPath(staticDir string) {
	staticDir = strings.ReplaceAll(staticDir, "\\", "/")
	staticDir = strings.TrimRight(staticDir, "/")
	index := strings.LastIndex(staticDir, "/")
	if index < 0 {
		staticDir = "/" + staticDir
		index = 0
	}
	staticPrefix := staticDir[index:]
	config.SetStaticMapping(staticPrefix+"/**", staticDir)
}
